<?php
namespace modules\iChat\services;

use modules\iChat\libs\FS;
use modules\iChat\objects\User;
use modules\iChat\objects\Session;
use modules\iChat\iChatException;

class HomeService
{
	public static function login($uid, $passwd)
	{
		$user = FS::search($uid, User::FILE_USER);
		if (!$user || $user['passWord'] != $passwd) {
			iChatException::throw(iChatException::USER_LOGIN_VERIFY_FAIL);
		}
		$user['token'] = $token = md5(json_encode($user));
		FS::puts($token, ['uid' => $user['uid'], 'time' => time() ], User::FILE_USER_ONLINE);
		return $user;
	}

	public static function checkUserLogin($token)
	{
		$user = FS::search($token, User::FILE_USER_ONLINE);
		if (!$user) {
			iChatException::throw(iChatException::USER_INFO_UNUSUAL);
		}
	}

	public static function getUser($token, $uid)
	{
		
	}
	
	public static function logout($token)
	{
		self::checkUserLogin($token);
		$ret = FS::delete($token, User::FILE_USER_ONLINE);
		if (!$ret) {
			iChatException::throw(iChatException::USER_INFO_UNUSUAL);
		}
		$user = FS::search($uid, User::FILE_USER);
		$uid  = $user['uid'];
	}

	public static function register($uid, $info)
	{
		FS::puts($uid, $info, User::FILE_USER);
	}

	public static function execute($method, $params)
	{
		if (!method_exists(__CLASS__, $method)) {
			iChatException::throw(iChatException::METHOD_REQUEST_NOT_EXISTS);
		}
		self::$method($params);
	}

	public static function alterInfo()
	{
		
	}
	public static function addFriend($token, $uid, $aid)
	{
		//检测用户信息
		self::checkUserLogin($token);
		$add = [
			'uid' => $aid,
			'time' => time(),
		];
		$friends = FS::search($uid, User::FILE_FRIEND);
		if ($friends) {
			FS::delete($uid, User::FILE_FRIEND);
			array_push($friends, $add);//二维数组
			FS::puts($uid, $friends, User::FILE_FRIEND);
		} else {
			FS::puts($uid, [$add], User::FILE_FRIEND);
		}
	}

	public static function delFriend()
	{

	}

	public static function mkGroup()
	{

	}

	public static function rmGroup()
	{

	}

	public static function getChatList($token, $uid)
	{
		self::checkUserLogin($token);
		return User::getChatList($uid);
	}

	public static function getFriendList($token, $uid)
	{
		self::checkUserLogin($token);
		return User::getFriendList($uid);
	}

	public static function testWriteUser()
	{
		$message = [
			'rid'  => "13122762997",
			'fid'  => "18612402834",
			'type' => 1,
			'msg'  => 'you good boy!',
			'time' => time(),
		];
		$rid = $message['rid'];

		$message = [
			'rid'  => "13122762997",
			'fid'  => "18612402834",
			'type' => 1,
			'msg'  => 'i meet you just now',
			'time' => time(),
		];
		$rid = $message['rid'];

		$message2 = [
			'rid'  => "13122762997",
			'fid'  => "15800586798",
			'type' => 1,
			'msg'  => 'where we going',
			'time' => time(),
		];

		$message = [
			'rid'  => "13122762997",
			'fid'  => "15800586798",
			'type' => 1,
			'msg'  => 'where are you!',
			'time' => time(),
		];
		$rid = $message['rid'];

		$message3 = [
			'rid'  => "13122762997",
			'fid'  => "15800586798",
			'type' => 1,
			'msg'  => 'it always a good time',
			'time' => time(),
		];
		$rid = $message['rid'];
		$rid = $message['rid'];
		FS::puts($rid, $message, Session::FILE_DIALOGUE_USER, false);
		//FS::puts($rid, $message, Session::FILE_DIALOGUE_GROUP, false);
	}
}